package com.wy.Service.impl;

import com.wy.Service.HuzuService;
import com.wy.dao.HuzuDao;
import com.wy.dao.ResideDao;
import com.wy.pojo.Huzu;
import com.wy.pojo.Reside;
import com.wy.vo.HuzuDto;
import com.wy.vo.PageVo;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("huzuService")
@Transactional
public class HuzurviceImpl extends BaseServiceImpl  implements HuzuService {

    @Resource
    private HuzuDao huzuDao;

    @Resource
    private ResideDao resideDao;

    @Override
    public PageVo searchAll(Integer current, Integer pageSize) {
        PageRequest pageRequest= PageRequest.of(current-1,pageSize);
        Page page= huzuDao.findAll(pageRequest);
        return new PageVo(page);
    }

    @Override
    public PageVo searchAll(Integer current, Integer pageSize, HuzuDto dto) {
        StringBuilder sbd=new StringBuilder("select r from Huzu r  where 1=1 ");
        if(!StringUtils.isEmpty(dto.getName())){
            sbd.append(" and r.name like :name");
            dto.setName("%"+dto.getName()+"%");
        }
        if(!StringUtils.isEmpty(dto.getResideid())){
            sbd.append(" and r.Resideid = :Resideid");
            dto.setResideid(dto.getResideid());
        }

        return super.searchPagination(current,pageSize,sbd.toString(),dto);
    }

    @Override
    public List<Reside> searchReside() {
        return resideDao.findAll();
    }

    @Override
    public void add(Huzu huzu) {
        huzuDao.save(huzu);
    }

    @Override
    public List<Huzu> listAll() {
        return huzuDao.findAll();
    }

    @Override
    public void delete(Integer id) {
        huzuDao.deleteById(id);
    }

    @Override
    public void update(Huzu huzu) {
        huzuDao.save(huzu);
    }

}
